﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.pro2e.console.service.auth.vo;
using com.pro2e.console.service.auth;
using com.pro2e.web.util.page;
using com.pro2e.console.core.hibernate;
using com.pro2e.web.config.init;
using NHibernate;
using com.pro2e.console.service.msg.vo;

public partial class adm_UC14_UC14_1_UserAdd : System.Web.UI.Page
{
    log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    AuthService authService = new AuthService();

    protected void Page_Load(object sender, EventArgs e)
    {
       // txtArrivedYear.Text = (DateTime.Today.Year - 1911).ToString();
        //新增模式
        loadVoToUI("admin");
        btnReset.Visible = false;
      //  ToInsertMode();
    }

    private void ToInsertMode()
    {

        btnUpdate.Visible = true;
        btnAdd.Visible = false;
        txtId.ReadOnly = false;
    }

    private void ToUpdateMode()
    {
        btnUpdate.Visible = true;
        btnAdd.Visible = false;
        txtId.ReadOnly = false;
    }

  
  
    private void clearInput()
    {
        txtId.Text = string.Empty;
        txtFullNameInChinese.Text = string.Empty;
        txtFullNameInEnglish.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtAddress.Text = string.Empty;

        hdnVersion.Value = string.Empty;
        rdbIsValidAccount.SelectedValue = "1";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
 

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        lblMsg.Text = "";

        switch (e.CommandName)
        {
            case "MyEdit":

                string userId = e.CommandArgument.ToString();
                log.Debug("get UserId=" + userId);
                loadVoToUI(userId);
                ToUpdateMode();
                break;

            case "Select":
                log.Debug("commandName= Select,get UserId=" + e.CommandArgument.ToString());
                Response.Redirect("~/admin/UC14/UserRoleSet.aspx?UserId=" + e.CommandArgument.ToString());
                break;
        }
    }


    private void loadVoToUI(string userId)
    {

        LoginUser user = authService.LoadLoginUser(userId);
        if (user.UserId.ToLower() == "admin")
        {
            btnUpdate.Enabled = true;
            btnReset.Enabled = false;
        }
        else 
        {
            btnUpdate.Enabled = true;
            btnReset.Enabled = false;
        }
        txtId.Text = user.UserId;
 
        txtFullNameInChinese.Text = user.FullNameInChinese;
        txtFullNameInEnglish.Text = user.FullNameInEnglish;
        hdnVersion.Value = user.Version.ToString();
        rdbIsValidAccount.SelectedValue = user.IsAlive.ToString();
        txtEmail.Text = user.Email;
        txtAddress.Text = user.ContactAddress;
     

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblMsg.Text = string.Empty;
        //轉成小寫
        string id = txtId.Text.Trim().ToLower();
        string nameInChinese = txtFullNameInChinese.Text.Trim();
        string nameInEnglish = txtFullNameInEnglish.Text.Trim();
        int IsAlive = int.Parse(rdbIsValidAccount.SelectedValue);
        string email = txtEmail.Text.Trim();
        string address = txtAddress.Text.Trim();
              LoginUser user = authService.LoadLoginUser(id);

        if (user != null)
        {
            lblMsg.Text = MsgVO.USER_ALREADY_EXIST;
            return;
        }
        else
        {
            LoginUser newUser = new LoginUser();
            newUser.UserId = id;
        
            newUser.FullNameInChinese = nameInChinese;
            newUser.FullNameInEnglish = nameInEnglish;
            newUser.IsAlive = IsAlive;
            newUser.Email = email;
            newUser.ContactAddress = address;
            newUser.Password = txtPw.Text.Trim();
            authService.AddLoginUser(newUser);
            lblMsg.Text = MsgVO.INSERT_OK;
            clearInput();
            //GridView1.DataBind();
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string id = txtId.Text;
        string nameInChinese = txtFullNameInChinese.Text.Trim();
        string nameInEnglish = txtFullNameInEnglish.Text.Trim();
        string version = hdnVersion.Value;
        int isAlive = int.Parse(rdbIsValidAccount.SelectedValue);
        string email = txtEmail.Text.Trim();
        string pwd=txtPw.Text.Trim();
        string address = txtAddress.Text.Trim();
        int height = 0;
        int weight = 0;
        LoginUser user = authService.LoadLoginUser(id);
        log.Debug("old Versin=" + version);
        log.Debug("new Versin=" + user.Version);
        user.FullNameInChinese = nameInChinese;
        user.FullNameInEnglish = nameInEnglish;
        user.Version = Int32.Parse(version);
        user.IsAlive = isAlive;
        user.Email = email;
        user.ContactAddress = address;
        user.Height = height;
        user.Weight = weight;
        user.Password = pwd;
        try
        {
            authService.UpdateUser(user);

            lblMsg.Text = MsgVO.UPDATE_OK;

            //clearInput();
           // ToInsertMode();
        //    GridView1.DataBind();
        }
        catch (StaleObjectStateException ex)
        {
            log.Info(ex);
            lblMsg.Text = MsgVO.STALE_EXCEPTION_MSG;
            clearInput();
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        clearInput();
    }
}
